package com.hex.ds.hdtp.core.web.data.contrast.controller;

import cn.hutool.core.util.StrUtil;
import com.hex.bigdata.hbda.annotation.Log;
import com.hex.ds.hdtp.core.app.common.page.dto.PageDto;
import com.hex.ds.hdtp.core.app.data.contrast.dto.request.CrossDbContrastTaskRequestDto;
import com.hex.ds.hdtp.core.app.data.contrast.service.ICrossDbContrastTaskService;
import com.hex.ds.hdtp.core.web.common.page.PageVo;
import com.hex.ds.hdtp.core.web.common.page.converter.IPageVoAndPageDtoConverter;
import com.hex.ds.hdtp.core.web.data.contrast.converter.ICrossDbContrastTaskVoConverter;
import com.hex.ds.hdtp.core.web.data.contrast.vo.CrossDbContrastTaskVo;
import com.hex.ds.hdtp.core.web.data.contrast.vo.query.CrossDbContrastTaskQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.constraints.NotNull;
import java.util.HashMap;
import java.util.List;

/**
 * @Package: com.hex.ds.hdtp.core.web.data.contrast.controller
 * @ClassName CrossDbContrastTaskController
 * @Description: 跨库对比任务-WEB接口
 * @Author: wz.li
 * @Date 2023/7/24 18:01
 * @Version v2.0.2
 **/
@Slf4j
@RestController
@Api(tags = "05-跨库比对：任务")
@RequestMapping("/api/cross/db/contrast/task")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class CrossDbContrastTaskController {

    private final ICrossDbContrastTaskService crossDbContrastTaskService;

    private final ICrossDbContrastTaskVoConverter converter;

    private final IPageVoAndPageDtoConverter pageVoAndPageDtoConverter;

    /*
     * @Method: queryListByPage <br>
     * @Param: [crossDbContrastTaskQueryVo, pageVo] <br>
     * @Return: org.springframework.http.ResponseEntity <br>
     * @Description：跨库对比任务-分页查询<br>
     * @Author： wz.li<br>
     * @Date： 2023/7/24 18:02 <br>
     * @Version： V2.0.2<br>
     */
    @GetMapping
    @ApiOperation("跨库对比任务-分页查询")
    public ResponseEntity queryListByPage(CrossDbContrastTaskQueryVo crossDbContrastTaskQueryVo, PageVo pageVo) {
        log.debug("进入到【CrossDbContrastTaskController.queryListByPage】方法,参数：{}" + crossDbContrastTaskQueryVo.toString());
        if (StrUtil.isNotBlank(crossDbContrastTaskQueryVo.getCrtTs())) {
            String[] split = crossDbContrastTaskQueryVo.getCrtTs().split(",");
            crossDbContrastTaskQueryVo.setCrtTs(split[0]);
            crossDbContrastTaskQueryVo.setEndTs(split[1]);
        }
        final PageDto pageDto = pageVoAndPageDtoConverter.toPageDto(pageVo);
        CrossDbContrastTaskRequestDto requestDto = converter.toRequestDtoByQuery(crossDbContrastTaskQueryVo);
        return ResponseEntity.ok(crossDbContrastTaskService.queryListByPage(requestDto, pageDto));
    }

    /*
     * @Method: add <br>
     * @Param: [crossDbContrastTaskVo] <br>
     * @Return: org.springframework.http.ResponseEntity <br>
     * @Description：跨库对比任务-新增<br>
     * @Author： wz.li<br>
     * @Date： 2023/7/24 18:02 <br>
     * @Version： V2.0.2<br>
     */
    @Log("跨库对比任务-新增")
    @PostMapping
    @ApiOperation("跨库对比任务-新增")
    public ResponseEntity add(@RequestBody CrossDbContrastTaskVo crossDbContrastTaskVo) {
        log.info("进入到【CrossDbContrastTaskController.add】方法,参数：{}", crossDbContrastTaskVo.toString());
        return ResponseEntity.ok(crossDbContrastTaskService.addWithBackId(converter.toRequestDto(crossDbContrastTaskVo)));
    }

    /*
     * @Method: update <br>
     * @Param: [crossDbContrastTaskVo] <br>
     * @Return: org.springframework.http.ResponseEntity <br>
     * @Description：跨库对比任务-更新<br>
     * @Author： wz.li<br>
     * @Date： 2023/7/24 18:02 <br>
     * @Version： V2.0.2<br>
     */
    @Log("跨库对比任务-更新")
    @PutMapping
    @ApiOperation("跨库对比任务-更新")
    public ResponseEntity update(@RequestBody CrossDbContrastTaskVo crossDbContrastTaskVo) {
        log.info("进入到【CrossDbContrastTaskController.update】方法,参数：{}", crossDbContrastTaskVo.toString());
        crossDbContrastTaskService.modifyById(converter.toRequestDto(crossDbContrastTaskVo));
        return ResponseEntity.ok("更新成功");
    }

    /*
     * @Method: delete <br>
     * @Param: [pkIds] <br>
     * @Return: org.springframework.http.ResponseEntity <br>
     * @Description：跨库对比任务-批量删除<br>
     * @Author： wz.li<br>
     * @Date： 2023/7/24 18:02 <br>
     * @Version： V2.0.2<br>
     */
    @Log("跨库对比任务-批量删除")
    @DeleteMapping(value = {"/{pkIds}"})
    @ApiOperation("跨库对比任务-批量删除")
    public ResponseEntity delete(@PathVariable("pkIds") @NotNull List<String> pkIds) {
        log.info("进入【CrossDbContrastTaskController.delete】方法,参数：" + pkIds);
        crossDbContrastTaskService.batchRemoveById(pkIds);
        return ResponseEntity.ok("删除成功");
    }

    /**
     * @Method contrast
     * @Param dataRange
     * @Param pkIds
     * @Return org.springframework.http.ResponseEntity
     * @Description 跨库对比任务-对比
     * @Author gj.xu
     * @Date 2024/2/18 10:59
     * @Version V2.0
     */
    @Log("跨库对比任务-对比")
    @PutMapping(value = {"/contrast/{dataRange}/{pkIds}"})
    @ApiOperation("跨库对比任务-对比")
    public ResponseEntity contrast(@PathVariable("dataRange") @NotNull String dataRange,
                                   @PathVariable("pkIds") @NotNull List<String> pkIds) {
        log.info("进入【CrossDbContrastTaskController.contrast】方法,数据范围: {}, 主键：{}", dataRange, pkIds);
        crossDbContrastTaskService.contrast(dataRange, pkIds);
        return ResponseEntity.ok("操作成功");
    }

    @Log("跨库对比任务-导入")
    @PostMapping(value = {"/import"})
    @ApiOperation("跨库对比任务-导入")
    public ResponseEntity importContrast(@RequestBody MultipartFile file) {
        log.info("进入【CrossDbContrastTaskController.importContrast】方法,参数{}", file);
        HashMap<String,Object> resultMap = crossDbContrastTaskService.importContrast(file);
        return new ResponseEntity(resultMap, HttpStatus.OK);
    }

    @Log("跨库对比任务-导出")
    @GetMapping(value = {"/export"})
    @ApiOperation("跨库对比任务-导出")
    public ResponseEntity export(@RequestParam List<String> pkIds) {
        log.info("进入【CrossDbContrastTaskController.export】方法,参数{}", pkIds);
        ByteArrayResource bar = crossDbContrastTaskService.export(pkIds);
        return  ResponseEntity.ok()
                .contentType(MediaType.APPLICATION_OCTET_STREAM)
                .header("Content-disposition", "attachment; filename=SourceInfoTemplate")
                .body(bar);
    }
}
